package com.thirtywords.cleaner;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;

import com.thirtywords.analizer.beans.Link;

public class SimpleCleaner extends Cleaner {

	public SimpleCleaner(String url) {
		super(url, 0);
	}

	@Override
	public String doClean() {

		StringBuilder builder = new StringBuilder();

		System.out.println(">>>>>>>>>>>>>>>" + code.getUrl());
		SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss.SSS");
		Date d;

		d = new Date();
		System.out.println(s.format(d));
		System.out.println("Orginal " + code.getOriginalCode().length());

		String body = getBody(code.getOriginalCode());

		d = new Date();
		System.out.println(s.format(d));
		System.out.println("Body " + body.length());

		d = new Date();
		System.out.println(s.format(d));
		if (body != null) {

			HashSet<String> invalidElements = new HashSet<String>();
			invalidElements.add(HTMLElementName.ACRONYM);
			invalidElements.add(HTMLElementName.SUB);
			invalidElements.add(HTMLElementName.SUP);
			invalidElements.add(HTMLElementName.IMG);
			invalidElements.add(HTMLElementName.TITLE);
			invalidElements.add(HTMLElementName.FORM);
			invalidElements.add(HTMLElementName.SCRIPT);
			invalidElements.add(HTMLElementName.ASIDE);
			invalidElements.add(HTMLElementName.META);
			
			try {

				String line = "";

				List<Element> elements = getElements(code);

				for (Element element : elements) {

					line = "----0" + element.toString();
					System.out.println(line);

					builder.append(line);
					
					line = "----1" + element.getContent().getTextExtractor();
					System.out.println(line);

					builder.append(line);

					if (element.getName().equals(HTMLElementName.A)) {

						Link link = new Link();
						link.setHref(element.getAttributeValue("href"));
						link.setText(element.getTextExtractor().toString()
								.trim());
					}
				}
			} catch (Exception e) {

			}
		}
		d = new Date();
		System.out.println(s.format(d));
		System.out.println("Links " + builder.length());

		System.out.println(">>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<");

		try {

			FileWriter fstream = new FileWriter("c:/workspace/out.txt");
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(builder.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return builder.toString();
	}
}
